package com.uy.system.mapper;

import com.uy.system.model.entity.SysMenu;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.uy.system.model.vo.SysMenuTreeVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 菜单权限表 Mapper 接口
 * </p>
 *
 * @author ly
 * @since 2024-03-22
 */
public interface SysMenuMapper extends BaseMapper<SysMenu> {

    /**
     * 根据用户id获取用户对应权限
     * @param userId
     * @return
     */
    @Select("""
        SELECT sm.menu_id AS menuId,sm.menu_name AS menuName,sm.parent_id AS parentId,sm.path,sm.component,sm.menu_type AS menuType,sm.icon FROM sys_menu sm
        JOIN sys_role_menu sru ON sru.menu_id = sm.menu_id
        JOIN sys_role sr ON sru.role_id = sr.role_id
        JOIN sys_user_role sur ON sur.role_id = sr.role_id
        WHERE sr.status = 0 AND sr.del_flag = 0 AND sm.menu_type IN ('M','C') AND sm.status = 0 AND sur.user_id = #{userId}
    """)
    List<SysMenu> getMenusByUserId(@Param("userId") Long userId);

    /**
     * 获取菜单按钮操作权限
     * @param userId
     * @return
     */
    @Select("""
        SELECT sm.perms FROM sys_menu sm
        JOIN sys_role_menu srm ON sm.menu_id = srm.menu_id
        JOIN sys_user_role sur ON srm.role_id = sur.role_id
        WHERE sm.status = 0 AND sm.menu_type = 'F' AND sur.user_id = #{userId}
    """)
    List<String> getPermissionList(@Param("userId") Long userId);
}
